package org.yx.service.sqcm.save_ocv_standard;

import com.xiaoleilu.hutool.util.RandomUtil;
import com.xiaoleilu.hutool.util.StrUtil;
import org.yx.exception.BizException;
import org.yx.models.SqcmOcvStd;
import org.yx.utils.BaseService;
import org.yx.utils.FieldMap;

/**
 * 保存OCV工艺标准
 *
 * @author zxw
 * @since 2023/03/17
 **/
public class SqcmSaveOcvStandardService extends BaseService {
    public SqcmSaveOcvStandardOut saveOcvStandard(SqcmSaveOcvStandardIn in) throws Exception {
        if (in.getOcvStd() == null) {
            throw new BizException("SYS-0004", FieldMap.of("parameter", "ocvStd"));
        }

        SqcmOcvStd std = in.getOcvStd();
        std.setOrgCode(in.getUserOrgCode());
        if (StrUtil.isBlank(std.getId())) {
            if ("D".equals(std.getStdStatus())) {
                throw new BizException("BAS-0008", "数据不存在或已被删除。");
            }

            std.setId(RandomUtil.simpleUUID());

            insert(std);
        } else {
            SqcmOcvStd oldStd = selectWithPk(SqcmOcvStd.class, std.getId());
            copyProperties(std, oldStd);
            update(oldStd);
        }

        SqcmSaveOcvStandardOut out = new SqcmSaveOcvStandardOut();

        return out;
    }
}